package sms.dao;

import sms.entry.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {

    public User findForDengLu(String user, String password) {
        User user1 = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");    //注册驱动
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "select id,user,password from user where user =? and password = ?  limit 1";
            PreparedStatement ppst = conn.prepareStatement(sql);
            //获取键盘录入的数据,传入
            ppst.setObject(1,user);
            ppst.setObject(2,password);
            //查询数据库中信息
            ResultSet rs = ppst.executeQuery();
            if (rs.next()){
                int id = rs.getInt("id");
                String user2 = rs.getString("user");
                String password1 = rs.getString("password");
                user1 = new User(id, user2, password1);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return user1;
    }
}
